'''
Alternate implementation of rm_dups
using MD5 checksum.
'''

import os
import hashlib
from lib.configobj import ConfigObj

# Get root folder for source files
cfg = ConfigObj('tools.cfg')
root = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../sources')) + '/'

# Find all .go files
cmd = "find " + root + " -name '*.go'"
gofiles = os.popen(cmd).read().splitlines()

# Encountered checksums
checksums = set()
dups = []

def md5_checksum(filename):
  m = hashlib.md5()
  f = open(filename, 'rb')
  while True:
    # Don't read entire file at once
    data = f.read(10240)
    if len(data) == 0:
      break
    m.update(data)
  f.close()
  return m.hexdigest()

for gofile in gofiles:
  # Some repo folders named '*.go'
  if os.path.isdir(gofile):
    continue
  checksum = md5_checksum(gofile)
  if checksum in checksums:
    dups.append(gofile)
  else:
    checksums.add(checksum)

print "Number of duplicates encountered: " + str(len(dups))
for dup in dups:
  print "- " + dup
